﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.ajax
{
    public partial class news : System.Web.UI.Page
    {
        //BLL.Role rolebll = new BLL.Role();
        BLL.News newsbll = new BLL.News();
        Maticsoft.BLL.UserAdmin user = new Maticsoft.BLL.UserAdmin();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                {
                    Response.Write((newsbll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "confirm")
                {

                    Model.News ne = newsbll.GetModel(Convert.ToInt32(Request["id"]));
                    //ne.userstate = "已审核";
                    //row = userdealersbll.Update(ne) ? 2 : 0;
                    Response.Write(ne.newscontent);
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "confirm2")
                {

                    Model.News ne = newsbll.GetModel(Convert.ToInt32(Request["id"]));
                    ne.newstate = "已审核";
                    ne.audituser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                    ne.audittime = DateTime.Now; ;
                    row = newsbll.Update(ne) ? 2 : 0;
                    Response.Write(row);

                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "deletes")//批量删除
                {

                    foreach (var item in Request["ids"].Split(','))
                    {
                        newsbll.Delete(Convert.ToInt32(item));
                    }

                    Response.Write("1");
                }
                else
                {
                    Model.News ne = new Model.News();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        ne = newsbll.GetModel(Convert.ToInt32(Request["id"]));
                        ne.newstitile = Request["newstitile"];
                        ne.newscontent = Request["newscontent"];
                        ne.username = Request["username"];
                        ne.userid = Request["userid"];
                        row = newsbll.Update(ne) ? 2 : 0;
                    }
                    else
                    {
                        ne.createtime = DateTime.Now;
                        ne.createuser = user.GetModel(Convert.ToInt32(Request.Cookies["UserLogin"].Values[0])).username;
                        ne.newstitile = Request["newstitile"];
                        ne.newscontent = Request["newscontent"];
                        ne.username = Request["username"];
                        ne.userid = Request["userid"];
                        ne.newstate = "待审核";
                        ne.audituser = null;
                        ne.audittime = null;

                        row = newsbll.Add(ne) > 0 ? 1 : 0;
                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
            }
        }
    }
}